package com.lys.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lys.eduservice.entity.Chapter.ChapterVo;
import com.lys.eduservice.entity.Chapter.VideoVo;
import com.lys.eduservice.entity.EduChapter;
import com.lys.eduservice.entity.EduVideo;
import com.lys.eduservice.mapper.EduChapterMapper;
import com.lys.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lys.eduservice.service.EduVideoService;
import com.lys.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lys
 * @since 2022-06-30
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getAllChapterAndVideoById(String id) {
        //查询章节列表
        LambdaQueryWrapper<EduChapter> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(EduChapter::getCourseId,id);
        List<EduChapter> eduChapterList = baseMapper.selectList(queryWrapper1);


        //查询小结列表
        LambdaQueryWrapper<EduVideo> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(EduVideo::getCourseId,id);
        List<EduVideo> eduVideoList = eduVideoService.list(queryWrapper2);

        //章节列表信息存入chapterVo中
        ChapterVo chapterVo =null;
        List<ChapterVo> chapterVoList = new ArrayList<>();
        for (EduChapter eduChapter : eduChapterList) {
            chapterVo =new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把小结信息放入chapterVo的children中
            VideoVo videoVo =null;
            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideoList) {

                videoVo =new VideoVo();
                //获取此章节对应的所有小结
                if (eduVideo.getChapterId().equals(chapterVo.getId())){
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }

    @Override
    public boolean deleteChapterInfoById(String chapterId) {
        //判断章节中是否有小结
        LambdaQueryWrapper<EduVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getChapterId,chapterId);
        int count = eduVideoService.count(queryWrapper);
        if (count>0){
            throw new GuliException(20001,"请先删除小结！");
        }else {
            int result = baseMapper.deleteById(chapterId);
            //成功
            return result>0;
        }
    }

}
